import { watch } from 'vue'
import { useAdDialogStore } from '@/store/ad-dialog'
import { getCurrentDate } from '@/utils/string-helper'
import { useAdultChecked } from '@/compositions/utils/adultChecked'
import { POPUP_AD_DIALOG } from '@/constant'

export const useAdObserver = () => {
  const { $openAdDialog } = useAdDialogStore()

  const opener = {
    creatorCampaign: () => {
      $openAdDialog(POPUP_AD_DIALOG.CREATOR_CAMPAIGN).open()
    },
  }

  const executer = {
    creatorCampaign: () => {
      const hasShownPopupDate = localStorage.getItem('__POPUP_DATE')
      if (hasShownPopupDate !== getCurrentDate()) {
        opener.creatorCampaign()
        localStorage.setItem('__POPUP_DATE', getCurrentDate())
      }
    },
  }

  function watchOpenAd() {
    const { adultChecked } = useAdultChecked()

    watch(
      adultChecked,
      (newValue) => {
        if (newValue) {
          // 暫註釋，保持橫幅即可
          // executer.creatorCampaign()
        }
      },
      { immediate: true },
    )
  }

  return {
    opener,
    executer,
    watchOpenAd,
  }
}
